<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

/**
 * 国际税管理控制器
 * Class Intertax
 * @package app\admin\controller
 * @author ma <80234043@qq.com>
 * @date 2022/02/12 18:12
 */
class Intertax extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'SystemIntertax';

    //根据集团公司id获取子公司信息
    public function aipGetSubsidiarys(){
        //$group_company_id = $this->request->post('group_company_id');
        $group_company_id = 1;
        $subsidiarys = Db::name('SystemSubsidiary')->where(['group_company_id'=>$group_company_id])->column('subsidiary','id');
        return json(['data'=>$subsidiarys,'code'=>1,'message'=>'ok']);
    }

    //列表页
    public function index(){
        $this->title = '国际税管理';
        $get = $this->request->get();
        $db = Db::name($this->table)->where(['is_deleted' => '0']);
        foreach (['country', 'year'] as $key) {
            //(isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
            (isset($get[$key]) && $get[$key] !== '') && $db->where($key, $get[$key]);
        }
        if (isset($get['date']) && $get['date'] !== '') {
            list($start, $end) = explode('-', str_replace(' ', '', $get['date']));
            $db->whereBetween('create_at', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }
        return parent::_list($db);
    }

    //添加
    public function add()
    {
        return $this->_form($this->table, 'form',$pkField = '', $where = [], $extendData = ['create_at'=>time(),'create_by'=>session("user.id")]);
    }

    //修改
    public function edit(){
        return $this->_form($this->table, 'form',$pkField = '', $where = [], $extendData = ['create_at'=>time(),'create_by'=>session("user.id")]);
    }

    /**
     * 表单数据前缀方法
     * @param array $vo
     */
    protected function _form_filter(&$vo)
    {
        if ($this->request->isGet()) {
            //获取集团公司信息
            $groups = Db::name('SystemGroupCompany')->where(['is_deleted'=>0])->order('id desc')->select();
            $this->assign('groups', $groups);
            $subsidiary = Db::name('SystemSubsidiary')->select();
            $this->assign('subsidiary', $subsidiary);
        }
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        if(count($data)>0){
            //集团公司id
            $group_ids = array_column($data, 'group_company_id');
            //批量获取集团公司名称
            $group_company_names = Db::name('SystemGroupCompany')->where(['id'=>['IN',$group_ids]])->column('name','id');
            //子公司id
            $subsidiary_ids = array_column($data,'subsidiary_id');
            //批量获取子公司名称
            $subsidiarys = Db::name('SystemSubsidiary')->where(['id'=>['IN',$subsidiary_ids]])->column('subsidiary','id');

            $countrys = config('country_map');
            $business = config('business_map');
            foreach ($data as &$vo) {
                $vo['groupcompany'] = isset($group_company_names[$vo['group_company_id']]) ? $group_company_names[$vo['group_company_id']] :'';
                $vo['subsidiary'] = isset($subsidiarys[$vo['subsidiary_id']]) ? $subsidiarys[$vo['subsidiary_id']] :'';
                $vo['country'] = isset($countrys[$vo['country']]) ? $countrys[$vo['country']]: '';
                $vo['business'] = isset($business[$vo['business_id']]) ? $business[$vo['business_id']]: "";
            }

        }
    }

    //删除
    public function del(){
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }
}